<?php
/**
 * Ajax for Admin Login Class - PHP5
 *
 * Description:
 * This class handles the functionality for users login via Ajax
 *
 * @copyright	Copyright (c) eFun PMS 10th March, 2010.
 * @version 	1.0.0
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 **/
class ajax extends Controller
{
	var $c 		= 'ajax';//current controler
	var $p_c 	= 'siteadmin';//parent controller
	var $ajax_arr = array();
	/** 
	 * Welcome
	 *
	 * @desc contructor of the class
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function ajax()
	{
		parent::Controller();	
    
    $this->ajax_arr['suc'] = '';
    $this->ajax_arr['err'] = '';
    $this->ajax_arr['msg'] = '';
    $this->ajax_arr['act'] = '';
    
		//load login language
		$this->lang->load('login', ADMIN_LANGUAGE);		
	}
	
	/* ------------------------------------------------------------------------- */
	
	/** 
	 * Index
	 *
	 * @desc NA
	 * @access public/private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function index()
	{		
		exit;
	}
	
	/** 
	 * Check
	 *
	 * @desc this will validate the user with the DB
	 * @access public
	 * @param  NA	 
	 * @return mixed
	 */
	function check()
	{
		$this->load->library('form_validation');
		$login_status 	= false;
		$data 			= array();
		
    $data['valid_errors'] = $this->_val_form();		
    if(empty($data['valid_errors'])){
      $login_status 	= true;      
    }

    if($login_status == false){			
      $this->ajax_arr['err'] = 1;    
      $this->ajax_arr['msg'] = $data['valid_errors'];
    }else if(empty($_POST)){			
      $this->ajax_arr['err'] = 1;    
      $this->ajax_arr['msg'] = $this->lang->line('invalid_user');
    }else{
      $this->ajax_arr['suc'] = 1;
      $this->ajax_arr['msg'] = $this->lang->line('valid_user');
      $this->ajax_arr['act'] = site_url($this->p_c.'/dashboard'); 
    }
    
    $this->_ajax_wrapper();
	}
	  
  /** 
	 * Validation Form
	 *
	 * @descp NA
	 * @access private
	 * @param  NA	 
	 * @return array
	 */
	function _val_form()
	{
		$config =  array();
		$config[] = array(
						 'field'   => 'txt_username',
						 'label'   => 'username/email',
						 'rules'   => 'trim|required|xss_clean'
					  );
		$config[] =  array(
						 'field'   => 'txt_pwd',
						 'label'   => 'password',
						 'rules'   => 'trim|required|xss_clean'
					  );
		
		$this->form_validation->set_rules($config); 
		$this->form_validation->set_error_delimiters('<p>&raquo; ', '</p>');
		if ($this->form_validation->run() == false)
		{
			return validation_errors(); 
		}
		else
		{
			//check in database if users is valid
			$usr = $this->input->post('txt_username');
			$pwd = $this->input->post('txt_pwd');
			
			$qry = "SELECT  os_users_id
              FROM 
                ".TBL_USERS."	
              WHERE	
                os_users_email = ".$this->db->escape($usr)."
                AND
                os_users_pwd = ".$this->db->escape($pwd)."
                AND
                os_users_status = '1'				  			
              ";
      
			$handle = $this->db->query($qry);
			if($handle->num_rows() == 0)
			{
				$error = $this->lang->line('invalid_user');
				return '<p>'.$error.'</p>';
			}
			else
			{
				$qry = "SELECT 	u.*,g.os_users_grp_name
						FROM 
							".TBL_USERS."	u
						JOIN
							".TBL_USERS_GRP." g
						ON
							g.os_users_grp_id = u.os_users_grp_id	
						WHERE	
							os_users_email = ".$this->db->escape($usr)."
                AND
                os_users_pwd = ".$this->db->escape($pwd)."
                AND
                os_users_status = '1'
						";
				$handle = $this->db->query($qry);
				$set_data = array();
				if($handle->num_rows() > 0)
				{
					$row = $handle->row_array(); 
          $nams = (!empty($row['os_users_fname'])) ?$row['os_users_fname']: "Guest";          
          if($nams != 'Guest' && !empty($row['os_users_fname'])){
            $nams .= ' '.$row['os_users_lname'];
          }
          
					$set_data = array(
								'usr_id' 		=> $row['os_users_id'] ,
								'usr_name' 		=> $nams ,
								'usr_email' 	=> $row['os_users_email'] ,
								'usr_grp_id' 	=> $row['os_users_grp_id'] ,
								'usr_grp_name' 	=> $row['os_users_grp_name'],
                'usr_expiry' => '',
                'usr_siteadmin' => TRUE
								);	
				}
				
				$this->session->set_userdata('adminusr_info',$set_data);				
			}	
		}
	}	
	
	/** 
	 * Ajax Wrapper
	 *
	 * @descp This will send the arry in JSON format
	 * @access private
	 * @param  NA	 
	 * @return void
	 */
	function _ajax_wrapper()
	{		
		echo json_encode($this->ajax_arr);
	}
	
}

/* End of file welcome.php */
/* Location: ./system/application/modules/admin_login/controllers/ajax.php */
?>